package com.lingxu.base.system.orgManage.controller;

import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.config.LogInfoHandler;
import com.lingxu.base.system.orgManage.dto.OrgParamsDto;
import com.lingxu.base.system.orgManage.service.SystemOrgManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * @Title: SystemOrgManage
 * @Package: com.lingxu.base.system.orgManage.controller
 * @Author: ysj闫世杰
 * @Email: 1105324813@qq.com
 * @Date: Created in 2022/3/1 14:58
 * @Description:
 * @Version: 1.0.0
 */
@Api(tags = "组织架构管理")
@Validated
@RestController
@RequestMapping(value = "/system/org")
public class SystemOrgManageController {

    @Autowired
    private SystemOrgManageService systemOrgManageService;

    /**
     * @Author ysj闫世杰
     * @Description 查询地区
     * @Date 2022/3/3 14:46
     * @Param: code
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @ApiOperation("查询地区")
    @RequestMapping(value = "/findAreaByCode",method = RequestMethod.GET)
    public Result findAreaByCode(String code){
        return systemOrgManageService.findAreaByCode(code);
    }

    @ApiOperation("查询地区的父id")
    @RequestMapping(value = "/findParentId")
    public Result findParentId(String id){
        return systemOrgManageService.findParentId(id);
    }
    /**
     * @Author ysj闫世杰
     * @Description 查询所有的单位名称
     * @Date 2022/3/7 17:56
     * @Param:
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @ApiOperation("查询所有的单位名称")
    @RequestMapping(value = "/findAllOrg",method = RequestMethod.GET)
    public Result findAllOrg(String id) {
        return systemOrgManageService.findAllOrg(id);
    }

    /**
     * @Author ysj闫世杰
     * @Description 新增单位部门信息
     * @Date 2022/3/1 15:46
     * @Param: sysOrg
     * @Param: superviseId
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @ApiOperation("新增单位部门信息")
    @RequestMapping(value = "/addNewOrganizeData",method = RequestMethod.POST)
    public Result addNewOrganizeData(@RequestBody OrgParamsDto dto){
        //保存日志
        String content = "新增单位：【" + dto.getName() +"】";
        new LogInfoHandler().setLogInfoHandler(2,2, content);
        return systemOrgManageService.addNewOrganizeData(dto);
    }

    /**
     * @Author ysj闫世杰
     * @Description 查询树形结构的单位部门
     * @Date 2022/3/2 10:14
     * @Param: id
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @ApiOperation("查询树形结构的单位部门")
    @RequestMapping(value = "/findOrgManageTree",method = RequestMethod.GET)
    public Result findOrgManageTree(@NotNull(message = "参数[id]不可以为空") String id) {
        return systemOrgManageService.findOrgManageTree(id);
    }

    /**
     * @Author ysj闫世杰
     * @Description 查询树形结构的单位部门带人
     * @Date 2022/4/21 17:51
     * @Param: id
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @ApiOperation("查询树形结构的单位部门带人")
    @RequestMapping(value = "/findOrgManagePersonTree",method = RequestMethod.GET)
    public Result findOrgManagePersonTree(String id) {
        return systemOrgManageService.findOrgManagePersonTree(id);
    }

    @ApiOperation("查询部门下的所有用户")
    @RequestMapping(value = "/findOrgManagePersonList")
    public Result findOrgManagePersonList(Integer page,Integer limit,String id){
        return systemOrgManageService.findOrgManagePersonList(page,limit,id);
    }

    /**
     * @Author ysj闫世杰
     * @Description 修改单位部门信息
     * @Date 2022/3/2 10:15
     * @Param: sysOrg
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @ApiOperation("修改单位部门信息")
    @RequestMapping(value = "/updateOrganizeData",method = RequestMethod.POST)
    public Result updateOrganizeData(@RequestBody OrgParamsDto dto){
        //保存日志
        StringBuilder content = new StringBuilder("修改单位信息：");
        if(!StringUtils.isEmpty(dto.getDwlxModify())){
            content.append("修改单位类型：").append("【").append(dto.getDwlxModify()).append("为").append(dto.getDwlx()).append("】。");
        }
        if(!StringUtils.isEmpty(dto.getNameModify())){
            content.append("修改单位名称：").append("【").append(dto.getNameModify()).append("为").append(dto.getName()).append("】。");
        }
        if(!StringUtils.isEmpty(dto.getSimpleNameModify())){
            content.append("修改单位简称：").append("【").append(dto.getSimpleNameModify()).append("为").append(dto.getSimpleName()).append("】。");
        }
        if(!StringUtils.isEmpty(dto.getLinkPersonNameModify())){
            content.append("修改联系人：").append("【").append(dto.getLinkPersonNameModify()).append("为").append(dto.getLinkPerson()).append("】。");
        }
        if(!StringUtils.isEmpty(dto.getAddressModify())){
            content.append("修改单位地址：").append("【").append(dto.getAddressModify()).append("为").append(dto.getAddress()).append("】。");
        }
        if(!StringUtils.isEmpty(dto.getTelModify())){
            content.append("修改联系电话：").append("【").append(dto.getTelModify()).append("为").append(dto.getTelephone()).append("】。");
        }
        if(!StringUtils.isEmpty(dto.getLevelModify())){
            content.append("修改单位等级：").append("【").append(dto.getLevelModify()).append("为").append(dto.getOrgLevel()).append("】。");
        }
        new LogInfoHandler().setLogInfoHandler(2,3, content.toString());
        return systemOrgManageService.updateOrganizeData(dto);
    }

    /**
     * @Author ysj闫世杰
     * @Description 根据id查询单位信息
     * @Date 2022/3/2 11:08
     * @Param: id
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @ApiOperation("根据id查询单位信息")
    @RequestMapping(value = "/findOrganizeDataById",method = RequestMethod.GET)
    public Result findOrganizeDataById(String id) {
        return systemOrgManageService.findOrganizeDataById(id);
    }

    /**
     * @Author ysj闫世杰
     * @Description 删除单位信息
     * @Date 2022/3/2 10:21
     * @Param:
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @ApiOperation("删除单位信息")
    @RequestMapping(value = "/deleteOrganizeData",method = RequestMethod.GET)
    public Result deleteOrganizeData(@NotNull(message = "参数[id]不可以为空") String id, String name){
        //保存日志
        String content = "删除单位：【" + name +"】";
        new LogInfoHandler().setLogInfoHandler(2,4, content);
        return systemOrgManageService.deleteOrganizeData(id);
    }

    /**
     * @Author ysj闫世杰
     * @Description 查询列表
     * @Date 2022/6/16 19:00
     * @Param: page
     * @Param: limit
     * @Param: id
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @ApiOperation(value = "查询部门列表")
    @RequestMapping(value = "/findChildOrgList",method = RequestMethod.GET)
    public Result findChildOrgList(Integer page,Integer limit,String id){
        return systemOrgManageService.findChildOrgList(page,limit,id);
    }

    /**
     * @Author ysj闫世杰
     * @Description 手动排序
     * @Date 2022/6/20 16:22
     * @Param: id
     * @Param: sort
     * @Param: type
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @RequestMapping(value = "/moveTo")
    public Result moveTo(String id,Integer sort,String type,String company){
        String index = "上移";
        if("-1".equals(type)){
            index = "下移";
        }
        String content = "将监督单位：【" + company +"】" + index;
        new LogInfoHandler().setLogInfoHandler(2,3, content);
        return systemOrgManageService.moveTo(id,sort,type);
    }
}
